(function(a) {
	a.fn.bjqs = function(b) {
		var c = {}, d = {
			width : 665,
			height : 219,
			animation : "slide",
			animationDuration : 450,
			automatic : true,
			rotationSpeed : 5000,
			hoverPause : true,
			showControls : true,
			centerControls : true,
			nextText : "Next",
			prevText : "Prev",
			showMarkers : false,
			centerMarkers : true,
			keyboardNav : true,
			useCaptions : false
		}, e = this, f = e.find(".bjqs"), g = f.children("li"), h = g.length, i = false, j = false, k = 0, l = 1, m = 0, n = g.eq(k), o = "forward", p = "backward";
		c = a.extend({}, d, b);
		g.css({
			height : c.height,
			width : c.width
		});
		f.css({
			height : c.height,
			width : c.width
		});
		e.css({
			height : c.height,
			width : c.width
		});
		g.addClass("bjqs-slide");
		if(c.showControls && h > 1) {
			var q = a('<ul class="bjqs-controls" ></ul>'), 
			r = a('<li style="border-radius:8px 0 0 8px;display:none;" class="bjqs-next" ><span></span></li>'), 
			s = a('<li style="border-radius:0 8px 8px 0;display:none;" class="bjqs-prev" ><span ></span></li>');
			r.click(function(a) {a.preventDefault();
				if(!i) {A(o, false)
				}
			});
			s.click(function(a) {a.preventDefault();
				if(!i) {A(p, false)
				}
			});
			r.appendTo(q);
			s.appendTo(q);
			q.appendTo(e);
			if(c.centerControls) {
				var t = r.children("a"), u = (e.height() - t.height()) / 2 - 15;
				r.children("a").css({'top':u}).show();
				s.children("a").css({'top':u}).show()
			}
		}
		if(c.showMarkers && h > 1) {
			var v = a('<ol class="bjqs-markers"></ol>'), w, x, u;
			a.each(g, function(b, d) {
				if(c.animType === "slide") {
					if(b !== 0 && b !== h - 1) {
						w = a('<li><span>' + b + "</span></li>")
					}
				} else {
					b++;
					w = a('<li><span>' + b + "</span></li>")
				}w.click(function(c) {c.preventDefault();
					if(!a(this).hasClass("active-marker") && !i) {A(false, b)
					}
				});
				w.appendTo(v)
			});
			x = v.children("li");
			x.eq(k).addClass("active-marker");
			v.appendTo(e);
			if(c.centerMarkers) {
				//u = (c.width - v.width()) / 2;
				u = '22px';
				v.css("right", u)
			}
		}
		if(c.keyboardNav && h > 1) {a(document).keyup(function(a) {
				if(!j) {clearInterval(z);
					j = true
				}
				if(!i) {
					if(a.keyCode === 39) {a.preventDefault();
						A(o, false)
					} else if(a.keyCode === 37) {a.preventDefault();
						A(p, false)
					}
				}
				if(j & c.automatic) {
					z = setInterval(function() {A(o)
					}, c.rotationSpeed);
					j = false
				}
			})
		}
		if(c.useCaptions) {a.each(g, function(b, c) {
				var d = a(c);
				var e = d.children("img:first-child");
				var f = e.attr("title");
				if(f) {
					var g = a('<p class="bjqs-caption">' + f + "</p>");
					g.appendTo(d)
				}
				e.removeAttr("title");
			})
		}
		if(c.hoverPause && c.automatic) {e.hover(function() {
				if(!j) {
				    clearInterval(z);
				    
//					$(".bjqs-next,.bjqs-caption,.bjqs-markers,.bjqs-prev").show();
                    $(this).find("li[class='bjqs-next'],li[class='bjqs-prev']").show();
                    $(this).find("ul[class='bjqs-controls']").css("width",$(this).width());
					//$(".bjqs-prev").show();
					
					j = true
				}
			}, function() {
				if(j) {
					z = setInterval(function() {A(o)
					}, c.rotationSpeed);
					$(this).find("li[class='bjqs-next'],li[class='bjqs-prev']").hide();
					   // $(".bjqs-next").hide();
					   // $(".bjqs-prev").hide();
					
					j = false
				}
			})
		}
		if(c.animation === "slide" && h > 1) {
			$first = g.eq(0);
			$last = g.eq( h - 1);
			$first.clone().addClass("clone").removeClass("slide").appendTo(f);
			$last.clone().addClass("clone").removeClass("slide").prependTo(f);
			g = f.children("li");
			h = g.length;
			$wrapper = a('<div class="bjqs-wrapper"></div>').css({
				width : c.width,
				height : c.height,
				overflow : "hidden",
				position : "relative"
			});
			f.css({
				width : c.width * h,
				left : -c.width
			});
			g.css({
				"float" : "left",
				position : "relative",
				display : "list-item"
			});
			$wrapper.prependTo(e);
			f.appendTo($wrapper)
		}
		var y = function(a) {
			if(c.animation === "fade") {
				if(a === o) {!n.next().length ? m = 0 : m++
				} else if(a === p) {!n.prev().length ? m = h - 1 : m--
				}
			}
			if(c.animation === "slide") {
				if(a === o) {
					m = l + 1
				}
				if(a === p) {
					m = l - 1
				}
			}
			return m
		};
		if(c.automatic && h > 1) {
			var z = setInterval(function() {A(o, false)
			}, c.rotationSpeed)
		}g.eq(k).show();
		f.show();
		var A = function(a, b) {
			if(!i) {
				if(a) {
					m = y(a)
				} else if(b && c.animation === "fade") {
					m = b - 1
				} else {
					m = b
				}
				i = true;
				if(c.animation === "fade") {
					if(c.showMarkers) {x.eq(k).removeClass("active-marker");
						x.eq(m).addClass("active-marker")
					}
					r = g.eq(m);
					n.fadeOut(c.animationDuration);
					r.fadeIn(c.animationDuration, function() {n.hide();
						k = m;
						n = r;
						i = false
					})
				} else if(c.animation === "slide") {
					if(c.showMarkers) {x.eq(l-1).removeClass("active-marker");
						if(m === h - 1) {x.eq(0).addClass("active-marker")
						} else if(m === 0) {x.eq(h-3).addClass("active-marker")
						} else {x.eq(m-1).addClass("active-marker")
						}
					}f.animate({
						left : -m * c.width
					}, c.animationDuration, function() {
						if(m === 0) {
							l = h - 2;
							f.css({
								left : -l * c.width
							})
						} else if(m === h - 1) {
							l = 1;
							f.css({
								left : -c.width
							})
						} else {
							l = m
						}
						i = false
					})
				}
			}
		};
		return this
	}
})(jQuery)